package wqh.websocket.alarm;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.*;
import wqh.global.GlobalSession;
import wqh.pojo.MapRet;
import wqh.websocket.AlarmConver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by devts on 2016/11/11.
 */
public class AlarmSocketHandler implements WebSocketHandler{

    private static final ArrayList<WebSocketSession> sessionList;

    @Autowired
    private AlarmConver alarmConver;

    static{
        sessionList = new ArrayList<WebSocketSession>();
    }

    public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
        if(webSocketSession != null){
            sessionList.add(webSocketSession);
            System.out.println("websocket上线:" + webSocketSession.getAttributes().get("sessionID"));
        }
    }

    public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage) throws Exception {

    }

    public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {
        if(webSocketSession.isOpen())
        {
            webSocketSession.close();
        }
        System.out.println("连接出现错误:"+throwable.toString());
        sessionList.remove(webSocketSession);
    }

    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {
        sessionList.remove(webSocketSession);
        System.out.println("websocket下线:" + webSocketSession.getAttributes().get("sessionID"));
    }

    public boolean supportsPartialMessages() {
        return false;
    }

    public synchronized void sendAlarmToWeb(TextMessage message){
        if(message != null){
            for (WebSocketSession session : sessionList) {
                try {
                    if (session.isOpen()) {
                        if(GlobalSession.existSessionID(session.getAttributes().get("sessionID").toString())){
                            session.sendMessage(new TextMessage(JSON.toJSONString(alarmConver.getAllStatusWeb("火警"))));
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public synchronized void cancelAlarmToWeb(Integer alarmID){
        if(alarmID != null){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("cancel_id",alarmID);
            for (WebSocketSession session : sessionList) {
                try {
                    if (session.isOpen()) {
                        session.sendMessage(new TextMessage(JSON.toJSONString(MapRet.DATA_OBJECT(map))));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public synchronized void sendLogOutToWeb(String sessionID){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("log_out",true);
        for (WebSocketSession session : sessionList) {
            try {
                if (session.isOpen()) {
                    if(sessionID.equals(session.getAttributes().get("sessionID").toString())){
                        session.sendMessage(new TextMessage(JSON.toJSONString(MapRet.DATA_OBJECT(map))));
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  /*  public synchronized void sendMessageToSessionList(TextMessage message) {
        for (WebSocketSession session : sessionList) {
            try {
                if (session.isOpen()) {
                    session.sendMessage(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/

}
